package com.basicsys.modules.util;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class Doc2HtmlUtil  {
	private static Doc2HtmlUtil doc2HtmlUtil;

    /**
     * 获取Doc2HtmlUtil实例
     */
    public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
        if (doc2HtmlUtil == null) {
            doc2HtmlUtil = new Doc2HtmlUtil();
        }
        return doc2HtmlUtil;
    }

    /**
     * 转换文件成html
     * 
     * @param fromFileInputStream:
     * @throws IOException 
     */
    public String file2Html(InputStream fromFileInputStream, String toFilePath,String type,String name,String bacsicpath) throws IOException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timesuffix = sdf.format(date);
        String docFileName = null;
        String htmFileName = null;
        if("doc".equals(type)){
            docFileName = "doc_" + name + ".doc";
            htmFileName = "doc_" + name + ".html";
        }else if("docx".equals(type)){
            docFileName = "docx_" + name + ".docx";
            htmFileName = "docx_" + name + ".html";
        }else if("xlsx".equals(type)){
            docFileName = "xlsx_" + name + ".xlsx";
            htmFileName = "xlsx_" + name + ".html";
        }else if("xls".equals(type)){
            docFileName = "xls_" + name + ".xls";
            htmFileName = "xls_" + name + ".html";
        }else if("ppt".equals(type)){
            docFileName = "ppt_" + name + ".ppt";
            htmFileName = "ppt_" + name + ".html";
        }else{
            return null;
        }

        File htmlOutputFile = new File(bacsicpath+"statics/images/notes/"+htmFileName);
        File docInputFile = new File(bacsicpath+"statics/images/notes/"+docFileName);
        if (htmlOutputFile.exists())
            htmlOutputFile.delete();
        htmlOutputFile.createNewFile();
        if (docInputFile.exists())
            docInputFile.delete();
        docInputFile.createNewFile();
        /**
         * 由fromFileInputStream构建输入文件
         */
        try {
            OutputStream os = new FileOutputStream(docInputFile);
            int bytesRead = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }

            os.close();
            fromFileInputStream.close();
        } catch (IOException e) {
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
        } catch (ConnectException e) {
            System.err.println("文件转换出错，请检查OpenOffice服务是否启动。");
        }
        // convert
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        // 转换完之后删除word文件
        docInputFile.delete();
        return htmFileName;
    }
}
